/**
 * 
 */
package com.gf.pf.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gf.common.BaseController;
import com.gf.common.ResultMessage;
import com.gf.common.UUIDGenerator;
import com.gf.pf.model.Department;
import com.gf.pf.model.Flexigrid;
import com.gf.pf.model.Ztree;
import com.gf.pf.service.DepartmentService;
import com.gf.pf.service.MyBatisService;

/**
 * @description 部门管理
 * @date   Mar 7, 2015
 */
@Controller
@RequestMapping("/pf/department")
public class DepartmentController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(DepartmentController.class);
	
	/**
	 * 部门列表
	 */
	private String department_list = "pf/departmentList";
	
	/**
	 * 添加部门
	 */
	private String department_add = "pf/departmentAdd";
	
	/**
	 * 修改部门
	 */
	private String department_modify = "pf/departmentModify";
	
	/**
	 * 每页记录数
	 */
	private String page_sql_id = "pf.department.depList";
	
	/**
	 * 总记录数
	 */
	private String total_sql_id = "pf.department.total";
	
	/**
	 * 父部门ID为空！
	 */
	private String parent_id_empty = "父部门ID为空！";
	
	/**
	 * 部门ID为空！
	 */
	private String department_id_empty = "部门ID为空！";
	
	@Autowired
	private DepartmentService departmentService;

	@Autowired
	private MyBatisService myBatisService;
	
	/**
	 * 
	 * @description 部门管理列表
	 * @return String
	 */
	@RequestMapping(value = "/flDepartment", method = RequestMethod.GET)
	public String departmentList() {
		return this.department_list;
	}
	
	/**
	 * 
	 * @description 部门管理列表数据
	 * @return Flexigrid
	 */
	@RequestMapping(value = "/dataGrid", method = RequestMethod.POST)
	@ResponseBody
	public Flexigrid dataGrid(Department dep, Flexigrid flex) {
		if(StringUtils.isEmpty(dep.getId())) {
			dep.setId(super.ROOT_ID);
		}
		flex.setObj(dep);
		flex.setRowStart((flex.getPage() - 1) * flex.getRp());
		flex.setRowEnd(flex.getPage() * flex.getRp());
		List<?> rows = myBatisService.getList(this.page_sql_id, flex);
		int total = myBatisService.count(this.total_sql_id, flex);
		flex.setTotal(total);
		flex.setRows(rows);
		return flex;
	}
	
	/**
	 * 
	 * @description 部门树形数据
	 * @return List<Ztree>
	 */
	@RequestMapping(value = "/ztree", method = RequestMethod.POST)
	@ResponseBody
	public List<Ztree> getDepartmentZtree() {
		return departmentService.getDepartmentZtree();
	}
	
	/**
	 * 
	 * @description 添加部门跳转
	 * @return String
	 */
	@RequestMapping(value = "/faDepartment", method = RequestMethod.GET)
	public String departmentAddForward(String pId, Model model) {
		if(!StringUtils.isEmpty(pId)) {
			Department dep = departmentService.getDep(pId);
			model.addAttribute("dep", dep);
		} else {
			LOGGER.error(this.parent_id_empty);
			return this.department_list;
		}
		return this.department_add;
	}
	
	/**
	 * 
	 * @description 添加部门
	 * @return String
	 */
	@RequestMapping(value = "/aDepartment", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage depAdd(Department dep) {
		if(StringUtils.isEmpty(dep.getDepartmentParentId())) {
			LOGGER.error(this.parent_id_empty);
		}
		
		dep.setId(UUIDGenerator.getInstance().getUUID32());
		Integer result = departmentService.saveDep(dep);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("部门保存成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("部门保存失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 
	 * @description 部门修改跳转
	 * @return String
	 */
	@RequestMapping(value = "/fmDepartment", method = RequestMethod.GET)
	public String depModifyForward(String depId, Model model) {
		if(!StringUtils.isEmpty(depId)) {
			Department dep = departmentService.getDep(depId);
			model.addAttribute("dep", dep);
		} else {
			LOGGER.error(this.department_id_empty);
			return this.department_list;
		}
		return this.department_modify;
	}
	
	/**
	 * 
	 * @description 部门修改
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/mDepartment", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage depModify(Department dep) {
		if(StringUtils.isEmpty(dep.getId())) {
			LOGGER.error(this.department_id_empty);
		}
		
		Integer result = departmentService.modifyDep(dep);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("部门修改成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("部门修改失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 
	 * @description 删除部门
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/dDepartment", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage depDelete(String depId) {
		if(StringUtils.isEmpty(depId)) {
			LOGGER.error(this.department_id_empty);
		}
		
		Integer result = departmentService.deleteDep(depId);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("部门删除成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("部门删除失败！");
		}
		return super.resultMessage;
	}
	
}
